package cn.kent.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.HeadersExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class HeaderConfig {
    public static final String HEADER_QUEUE_NAME_NAME = "header_queue_name_name";
    public static final String HEADER_QUEUE_AGE_NAME = "header_queue_age_name";
    public static final String HEADER_EXCHANGE_NAME = "header_exchange_name";

    @Bean
    Queue headerNameQueue() {
        return new Queue(HEADER_QUEUE_NAME_NAME, true, false, false);
    }

    @Bean
    Queue headerAgeQueue() {
        return new Queue(HEADER_QUEUE_AGE_NAME, true, false, false);
    }

    @Bean
    HeadersExchange headerExchange() {
        return new HeadersExchange(HEADER_EXCHANGE_NAME, true, false);
    }

    @Bean
    Binding nameBinding() {
        return BindingBuilder.bind(headerNameQueue())
                .to(headerExchange())
                // 如果将来 消息头部 中包含 name 属性， 就匹配成功
                .where("name").exists();
    }

    @Bean
    Binding ageBinding() {
        return BindingBuilder.bind(headerAgeQueue())
                .to(headerExchange())
                // 如果将来 消息头部 中包含 age 属性 并且 age 属性值为 99 ， 就匹配成功
                .where("age")
                .matches(99);
    }
}
